<?php
include_once "Util.php";
class ManagerIUDS extends Util{
	
	/**
	 * Method insert Data.
	 * @param Array data
	 * @param table name
	 * @return 'Y'
	 */
	public function inserData($arrData, $tableName){
		$colunm = "";
		$value = "";
		if(!gettype($arrData) == "array"){
			exit();
		}
		$size = count($arrData);
		for ($i = 0; $i < $size; $i++) {
       		$colunm .= key($arrData).", ";
       		$value .= "'".current($arrData)."', ";
		    next($arrData);
		}
   		$colunm = substr($colunm,0,strlen($colunm)-2);
   		$value = substr($value,0,strlen($value)-2);
   		if(($colunm == "" || $colunm == null) && ($value == "" || $value == null)){
   			exit();
   		}
   		$sql = "INSERT INTO ".$tableName." (";
   		$sql .= $colunm.") ";
   		$sql .= "VALUES (";
   		$sql .= $value.") ";
   		//echo "sql => " . $sql. "<br>";
   		$result = $this->commitQuery($sql);
   		return $result; 
	}
	/**
	 * Method Update Data.
	 * @param Array data
	 * @param table name
	 * @param array data's primary key name and value
	 * @param condition create by user
	 * @return 'Y'
	 */
	public function updateData($arrData, $tableName, $arrPrimaryKey, $conditionFormUser){
		$value = "";
		$condition = " WHERE ";
		if(!gettype($arrData) == "array"){
			exit();
		}
		$size = count($arrData);		
		for ($i = 0; $i < $size; $i++) {
       		 $value .= key($arrData)." = '".current($arrData)."', ";
		    next($arrData);
		}
   		$value = substr($value,0,strlen($value)-2);
   		$sql = "UPDATE ".$tableName." SET ";
   		$sql .= $value." ";
   		if(gettype($arrPrimaryKey)=="array" && $arrPrimaryKey != null){
   				while ($value = current($arrPrimaryKey)) {
       		  		$condition .= key($arrPrimaryKey)." = '".$value."' AND";
   			 		next($arrPrimaryKey);
   				}
   			$condition = substr($condition,0,strlen($condition)-4);
   			$sql .= $condition;
   		}else if($conditionFormUser != "" || $conditionFormUser != null){
			$word  = "WHERE";
			$pos = strpos($conditionFormUser, $word);
   			if ($pos === false){
   				$conditionFormUser = " WHERE ".$conditionFormUser;
   				$sql .= $conditionFormUser;
			}else{
				$sql .= $conditionFormUser;
			}
   		}
   		//echo "sql => " . $sql. "<br>";
   		$result = $this->commitQuery($sql);
   		return $result; 
	}
	/**
	 * Method Delete Data.
	 * @param table name
	 * @param array data's primary key name and value
	 * @param condition create by user
	 * @return 'Y'
	 */
	function deleteData($tableName, $arrPrimaryKey, $conditionFormUser){
		$sql = "DELETE FROM ".$tableName." ";
		if(gettype($arrPrimaryKey)=="array" && $arrPrimaryKey != null){
			$condition = " WHERE ";
   				while ($value = current($arrPrimaryKey)) {
       		  		$condition .= key($arrPrimaryKey)." = '".$value."' AND";
   			 		next($arrPrimaryKey);
   				}
   			$condition = substr($condition,0,strlen($condition)-4);
   			$sql .= $condition;
   		}else if($conditionFormUser != "" || $conditionFormUser != null){
			$word  = 'WHERE';
			$pos = strpos($conditionFormUser, $word);
   			if ($pos === false){
   				$conditionFormUser = " WHERE ".$conditionFormUser;
   				$sql .= $conditionFormUser;
			}else{
				$sql .= $conditionFormUser;
			}
   		}
   		//echo "sql => " . $sql. "<br>";
   		$result = $this->commitQuery($sql);
   		return $result; 
	}
	/**
	 * Method Select Data.
	 * @param array colunm name,
	 * @param table name
	 * @param condition
	 * @param mode -> 1. Query Data row('1') 2. Query Data list('2')
	 * @param operater -> 1. "LIKE" 2. "="
	 * @return result Data
	 */
	function queryData($arrData, $tableName, $conditionFormUser, $mode, $operater){
		//SELECT colunmName FROM table [WHERE + condition]
		$colunm = "";
		$condition = "";
		if(gettype($arrData) == "array"){
			while ($colunmName = current($arrData)){
	       		  $colunm .= key($arrData).", ";;
	       		  if($operater == "LIKE"){
	       		  	$condition .= " ".key($arrData). " ".$operater." '%".$colunmName."%' AND";
	       		  }else{
	       		  	$condition .= " ".key($arrData). " = '".$colunmName."' AND";
	       		  }
	   			  next($arrData);
	   		}
	   		$colunm = substr($colunm,0,strlen($colunm)-2);
	   		$condition = substr($condition,0,strlen($condition)-4);
		}else{
			$colunm = $arrData;//String Data
		}
   		$sql = "SELECT ".$colunm." FROM ".$tableName."";
   		if($conditionFormUser != null || $conditionFormUser != ""){
   			$word  = " WHERE";
			$pos = strpos($conditionFormUser, $word);
   			if ($pos === false){
   				$conditionFormUser = " WHERE ".$conditionFormUser;
   				$sql .= $conditionFormUser;
			}else{
				$sql .= $conditionFormUser;
			}
   		}else{
   			if($condition != "" || $condition != null){
   				$sql .= " WHERE ".$condition;
   			}
   		}
   		//echo "sql => " . $sql. "<br>";
   		if($mode == "1"){
   			$arrayData = $this->fetchDataRow($sql);
   		}else{
   			$arrayData = $this->fetchDataList($sql);
   		}
   		return $arrayData; 
	
	}
	/**
	 * Method get fieldName form SQL command.
	 * @param SQL command
	 * @return array
	 */
	public function getFieldName($sql){
		$this->connect();
		$arrResult = array();
		$result = mysql_query($sql);
		if(!$result){
			echo 'Could not run query: ' . mysql_error();
			exit;
		}
		for($i=0;$i<mysql_num_fields($result);$i++){
			$arrResult[$i] =  mysql_field_name($result, $i);
		}	
		return $arrResult;
	}
	/**
	 * Method get list data  form SQL command.
	 * @param page
	 * @param table name
	 * @param id in table
	 * @return array
	 */
	public function listQueryLimit($page, $tableName,$idTable,$condition){
		$sqlCommant = "SELECT * FROM ".$tableName;
		if($condition != "" && $condition != null){
			$sqlCommant .= $condition;
		}
		$sqlCommant .=  " ".$this->queryLimit($page, $idTable);
		//echo $sqlCommant;
		$arrData = $this->fetchDataList($sqlCommant);
		return $arrData;
	} 
	
	/**
	 * Method get list data form SQL command have command JOIN.
	 * @param page
	 * @param sql command
	 * @param id in table
	 * @return array
	 */
	public function listQueryLimitWithJoin($page, $idTable,$sqlCommant){
		$sqlCommant .=  " ".$this->queryLimit($page, $idTable);
		$arrData = $this->fetchDataList($sqlCommant);
		return $arrData;
	} 
	
	/**
	 * Method get list All data  form SQL command.
	 * @param table name
	 * @return array
	 */
	public function listAllData($tableName){
		$sqlCommant = "SELECT * FROM ".$tableName."";
		$arrData = $this->fetchDataList($sqlCommant);
		return $arrData;
	} 
		public function listDataByYourSql($sqlCommant){
		$arrData = $this->fetchDataList($sqlCommant);
		return $arrData;
	} 
}

?>